// 搜索二维矩阵2

package Leetcode;

class Solution_240 {
    public boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length - 1;
        int n = matrix[0].length - 1;

        if (target > matrix[m][n] || target < matrix[0][0])
            return false;

        int startX = 0;
        int startY = n;
        while (startX < m && startY >= 0) {
            if (matrix[startX][startY] == target)
                return true;
            else if (matrix[startX][startY] > target) // 由于startY列的值都比matrix[startX][startY]大
                startY--;
            else //// 由于startX行的值都比matrix[startX][startY]小
                startX++;
        }
        return false;
    }
}